#! /bin/bash

frontend_node="reg.fastplusplus.cc/webtest/frontend_node@sha256:12245c396295cfcd70bc7988a42b77644bbb1b98d3752896194109f3e4c4e12d"
frontend_cluster="reg.fastplusplus.cc/webtest/frontend_cluster@sha256:af4b5d05dc6208fe165d329dcbee860d0341936ddc5c49a58d4c512fd0bfcd1d"

# 安装此版本依赖
install_rely() {
    apt install redis-server -y
}

upload_node_frontend() {
    # 刪除docker容器
    docker rm -f control_platform
    echo "删除节点容器成功"
    # 获取docker镜像id
    res=$(docker images | grep control_platform | awk '{print $3}')
    echo "删除镜像id为：${res}"
    docker rmi control_platform:beta
    echo "删除镜像tag成功"
    docker rmi ${res}
    echo "删除镜像成功,开始下载镜像"

    docker pull $frontend_node
    echo "下载镜像完成"
    # 修改刚下载的镜像tag
    imagesId=$(docker images | grep frontend_node | awk '{print $3}')
    docker tag $imagesId control_platform:beta
    docker run -itd -p 1234:1234 --restart=always --name control_platform control_platform:beta
    echo "更新节点前端完成"
}

upload_cluster_frontend() {
    # 刪除docker容器
    docker rm -f cluster_platform
    echo "删除集群容器成功"
    # 获取docker镜像id
    res=$(docker images | grep cluster_platform | awk '{print $3}')
    echo "删除镜像id为：${res}"
    docker rmi cluster_platform:beta
    echo "删除镜像tag成功"
    docker rmi ${res}
    echo "删除镜像成功，开始下载镜像"

    docker pull $frontend_cluster
    echo "下载镜像完成"
    # 修改刚下载的镜像tag
    imagesId=$(docker images | grep frontend_cluster | awk '{print $3}')
    docker tag $imagesId cluster_platform:beta
    docker run -itd -p 1235:1235 --restart=always --name cluster_platform cluster_platform:beta
    echo "更新集群前端完成"
}

upload_node() {
    scp fast@10.0.6.188:~/code/node/fast_backend/webconsole/server /home/fast/fast_backend/webconsole
    upload_node_frontend
}

upload_cluster() {
    scp fast@10.0.6.188:~/code/cluster/fast_5g_controller_backend/main /home/fast/cluster_backend
    upload_cluster_frontend
}

upload_all() {
    scp fast@10.0.6.188:~/code/node/fast_backend/webconsole/server /home/fast/fast_backend/webconsole
    scp fast@10.0.6.188:~/code/cluster/fast_5g_controller_backend/main /home/fast/cluster_backend
    upload_node_frontend
    upload_cluster_frontend
}

case $1 in
"upload_node")
    upload_node
    ;;
"upload_cluster")
    upload_cluster
    ;;
"upload_all")
    upload_all
    ;;
esac
